package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.converter.MaintenanceReminderConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.MaintenanceReminderDTO;
import com.neusoft.neuiotms.dcam.fixed.model.AssetType;
import com.neusoft.neuiotms.dcam.fixed.model.MaintenanceReminder;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.MaintenanceReminderRepository;
import com.neusoft.neuiotms.dcam.fixed.service.MaintenanceReminderService;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional
@Slf4j
public class MaintenanceReminderServiceImpl implements MaintenanceReminderService {

    @Autowired
    private MaintenanceReminderRepository reminderRepository;
    @Autowired
    private MaintenanceReminderConverter reminderConverter;
    @Autowired
    private AssetTypeRepository assetTyperepository;

    @Override
    public MaintenanceReminderDTO create(final MaintenanceReminderDTO dto) {
        final MaintenanceReminder model = this.reminderConverter.toModel(dto);
        if ((dto.getAssetTypeCodes() != null) && !dto.getAssetTypeCodes().isEmpty()) {
            final Searchable searchable = new Searchable();
            searchable.put("codes", dto.getAssetTypeCodes());
            final Page<AssetType> assetTypes = this.assetTyperepository.search(Pageable.unpaged(), searchable);
            if (assetTypes.getTotalElements() > 0) {
                model.setAssetTypes(assetTypes.getContent());
            }
        } else {
            MaintenanceReminderServiceImpl.log.info("新增 到期提醒设置至少包含一种资产类型");
            throw new UnitedException("到期提醒设置至少包含一种资产类型", new Object[] {});
        }
        this.reminderRepository.save(model);
        MaintenanceReminderServiceImpl.log.info("到期提醒 新增成功 ： " + dto.toString());
        return this.reminderConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        MaintenanceReminder model;
        try {
            model = this.reminderRepository.getOne(id);
            this.reminderRepository.delete(model);
            MaintenanceReminderServiceImpl.log.info("维保到期提醒 删除成功 ： id" + id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

    }

    @Override
    public MaintenanceReminderDTO update(final Long id, final MaintenanceReminderDTO dto) {
        MaintenanceReminder model;
        try {
            model = this.reminderRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        dto.setId(model.getId());
        model = this.reminderConverter.toModel(dto);
        if ((dto.getAssetTypeCodes() != null) && !dto.getAssetTypeCodes().isEmpty()) {
            final Searchable searchable = new Searchable();
            searchable.put("codes", dto.getAssetTypeCodes());
            final Page<AssetType> assetTypes = this.assetTyperepository.search(Pageable.unpaged(), searchable);
            if (assetTypes.getTotalElements() > 0) {
                System.out.println(assetTypes.getContent().getClass());
                model.setAssetTypes(new ArrayList<>(assetTypes.getContent()));
            }
        } else {
            MaintenanceReminderServiceImpl.log.info("修改 到期提醒设置至少包含一种资产类型");
            throw new UnitedException("到期提醒设置至少包含一种资产类型", new Object[] {});
        }
        this.reminderRepository.save(model);

        MaintenanceReminderServiceImpl.log.info("维保到期提醒 更新成功 id： " + id);
        return this.reminderConverter.toDTO(model);
    }

    @Override
    public List<MaintenanceReminderDTO> findAll() {
        final List<MaintenanceReminder> reminders = this.reminderRepository.findAll();
        final List<MaintenanceReminderDTO> reminderDtos = this.reminderConverter.toListDTO(reminders);
        return reminderDtos;
    }

    @Override
    public MaintenanceReminderDTO details(final Long id) {
        final MaintenanceReminder model;
        try {
            model = this.reminderRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        return this.reminderConverter.toDTO(model, false);
    }

    @Override
    public Page<MaintenanceReminderDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<MaintenanceReminder> models = this.reminderRepository.search(pageable, searchable);
        return this.reminderConverter.toPageDTO(models);
    }

}
